/**
 * @filename:SysApplicationController 2022年1月27日
 * @project Wonder xhadmin Framework  V1.0
 * Copyright(c) 2020 LuZhongren Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.admin.controller;


import com.wonder.admin.domain.SysApplication;
import com.wonder.admin.service.SysApplicationService;
import com.wonder.admin.request.SysApplicationRequest;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wonder.common.common.AjaxMap;
import com.wonder.common.common.AjaxResult;
import com.wonder.common.common.CommonPage;
import com.wonder.common.config.WonderConfig;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.utils.StringUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**   
 * 
 * <p>说明： 应用管理API接口层</P>
 * @version: V1.0
 * @author: LuZhongren
 * @time    2022年1月27日
 *
 */
@Api(tags = "应用管理")
@RestController
@RequestMapping("/admin/Sys.Base")
public class SysApplicationController {
	
	@Autowired
	private SysApplicationService sysApplicationService;
	
	@ApiOperation(value = "应用管理列表")
	@RequestMapping(value = "applicationList", method = RequestMethod.POST)
	public AjaxResult<CommonPage<SysApplication>> index(@RequestBody  SysApplicationRequest sysApplicationRequest) {
	
		Page<SysApplication> page = new Page<SysApplication>(sysApplicationRequest.getPage(), sysApplicationRequest.getLimit());
		IPage<SysApplication> list = sysApplicationService.selectList(sysApplicationRequest, page);
		return AjaxResult.success(CommonPage.restPage(list));
	}
	
	/**
	 * @explain 查询对象  <swagger GET请求>
	 * @param   对象参数：id
	 * @return  JsonResult
	 * @author  LuZhongren
	 * @time    2022年1月27日
	 */
	@GetMapping("/getApplicationInfo")
	@ApiOperation(value = "获取应用管理对象", notes = "作者：LuZhongren")
	public AjaxResult<SysApplication> getById(@RequestBody Map<String,Object> map){
	    if(map==null) AjaxResult.failed(MessageUtils.message("input.parameter.error"));
		Long app_id=Long.parseLong(map.get("app_id").toString());
		
		SysApplication sysApplication=sysApplicationService.getById(app_id);
		if (null!=sysApplication ) {
			 return AjaxResult.success(sysApplication);
		}else {
			 return AjaxResult.failed(MessageUtils.message("search.not.exist"));
		}
		
	}
	
	/**
	 * @explain 删除对象
	 * @param   对象参数：id
	 * @return  JsonResult
	 * @author  LuZhongren
	 * @time    2022年1月27日
	 */
	@PostMapping("/deleteApplication")
	@ApiOperation(value = "删除应用", notes = "作者：LuZhongren")
	public AjaxResult<SysApplication> deleteById(@RequestBody Map<String,Object> map){
	    if(map==null) AjaxResult.failed(MessageUtils.message("input.parameter.error"));
		Long app_id=Long.parseLong(map.get("app_id").toString());
		if(app_id==1)
			return AjaxResult.failed(MessageUtils.message("delete.not.permited"));
		
		SysApplication sysApplication=sysApplicationService.getById(app_id);
		
		if (null!=sysApplication) {
		  boolean rsg = sysApplicationService.removeById(app_id);
		  if (rsg) {
			    return AjaxResult.success(MessageUtils.message("delete.success"));
		  }else {
			   return AjaxResult.failed(MessageUtils.message("delete.fail"));
		  }
		}else {
			 return AjaxResult.failed(MessageUtils.message("delete.not.exist"));
		}
	}
	
	/**
	 * @explain 添加
	 * @param   对象参数：SysApplication
	 * @return  Boolean
	 * @author  LuZhongren
	 * @time    2022年1月27日
	 */
	@PostMapping("/createApplication")
	@ApiOperation(value = "添加应用", notes = "作者：LuZhongren")
	public AjaxResult<SysApplication> insert(@RequestBody SysApplication entity){

		if (null!=entity) {
			boolean rsg = sysApplicationService.save(entity);
			if (rsg) {
				  return AjaxResult.success(MessageUtils.message("insert.success"));
			  }else {
				  return AjaxResult.failed(MessageUtils.message("insert.fail"));
			  }
		}else {
			return AjaxResult.failed(MessageUtils.message("insert.parameter.error"));
		}
	}
	
	/**
	 * @explain 修改
	 * @param   对象参数：SysApplication
	 * @return  Boolean
	 * @author  LuZhongren
	 * @time    2022年1月27日
	 */
	@PostMapping("/updateApplication")
	@ApiOperation(value = "修改", notes = "作者：LuZhongren")
	public AjaxResult<SysApplication> update(@RequestBody SysApplication entity){
		if (null!=entity) {
			boolean rsg = sysApplicationService.updateById(entity);
			if (rsg) {
				  return AjaxResult.success(MessageUtils.message("update.success"));
			  }else {
				  return AjaxResult.failed(MessageUtils.message("update.fail"));
			  }
		}else {
			return AjaxResult.failed(MessageUtils.message("update.not.exist"));
		}
	}
	
	/**
	 * @explain 获取应用名 以及数据表名称
	 * @param   对象参数：SysApplication
	 * @return  Boolean
	 * @author  LuZhongren
	 * @time    2022年1月27日
	 */
	@PostMapping("/getAppInfo")
	@ApiOperation(value = "获取应用名 以及数据表名称", notes = "作者：LuZhongren")
	public Map<String,Object> getAppInfo(@RequestBody Map<String,Object> map){
	    if(map==null) AjaxResult.failed(MessageUtils.message("input.parameter.error"));
	    String controller_name=map.get("controller_name").toString();
	    String table_name=getTableName(controller_name);
	    String appFolder=WonderConfig.getAppFolder();
	    
	    Map<String,Object> retMap=new HashMap<String, Object>();
	    retMap.put("table_name", table_name);
	    retMap.put("pk", StringUtils.hasText(table_name)?table_name+"_id":"");
	    retMap.put("app_name", appFolder);
	    
	   return AjaxMap.success(retMap);
	}
	
	private String getTableName(String controller_name) {
		if(StringUtils.hasText(controller_name) && controller_name.indexOf('/')>0) {
			String tableName=StringUtils.split(controller_name,'/')[1];
			return tableName;
		}

		return controller_name;
	}
	
	
}